package com.library.bookmanagement.service.Impl;

import com.library.bookmanagement.entity.Admin;
import com.library.bookmanagement.entity.LoginUser;
import com.library.bookmanagement.entity.SuperAdmin;
import com.library.bookmanagement.entity.User;
import com.library.bookmanagement.exception.UnknowAccountException;
import com.library.bookmanagement.mapper.AdminMapper;
import com.library.bookmanagement.mapper.SuperAdminMapper;
import com.library.bookmanagement.mapper.UserMapper;
import com.library.bookmanagement.service.LoginService;
import com.library.bookmanagement.util.Constant;
import com.library.bookmanagement.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import java.util.Random;

/**
 * @author ：ZXM
 * @date ：Created in 2020/5/8 16:52
 * @description：登录service接口实现类
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SuperAdminMapper superAdminMapper;

    @Value("${spring.mail.username}")
    private String from;
    // 注入邮件处理Bean
    @Autowired
    private JavaMailSender javaMailSender;

    /**
    * @Description: 返回在注册验证码
    * @Param: [email]
    * @return: com.library.bookmanagement.util.ResultUtil<java.lang.String>
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public ResultUtil<String> getVertifyCode(String email) {
        User user = userMapper.selectByEmail(email);
        if(user != null){
            return ResultUtil.error().message("该邮箱已被注册，无法再次注册!");
        }
        // 随机生成验证码
        String checkCode = String.valueOf(new Random().nextInt(899999) + 100000);
        String message = "您的注册验证码为："+checkCode;


        // 创建单邮件对象并设置邮件内容
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(email);
        simpleMailMessage.setSubject(Constant.MEASSAGE_REGISTER_TITLE);
        simpleMailMessage.setText(message);
        // 发送邮件
        javaMailSender.send(simpleMailMessage);
        return ResultUtil.ok().data(checkCode);
    }

    /**
    * @Description: 后去登录用户
    * @Param: [username]
    * @return: com.library.bookmanagement.entity.LoginUser
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @Override
    public LoginUser queryUserByName(String username) {
        User user = userMapper.selectByName(username);
        LoginUser loginUser = new LoginUser();
        if(user == null){
            Admin admin = adminMapper.selectByName(username);
            if(admin == null){
                SuperAdmin superAdmin = superAdminMapper.selectByName(username);
                if(superAdmin == null){
                    throw new UnknowAccountException();
                }else{
                    loginUser.setUsername(superAdmin.getName());
                    loginUser.setPassword(superAdmin.getPassword());
                    loginUser.setEmail(superAdmin.getSuperEmail());
                    loginUser.setRole(3);
                }
            }else{
                loginUser.setUsername(admin.getName());
                loginUser.setPassword(admin.getPassword());
                loginUser.setEmail(admin.getaEmail());
                loginUser.setRole(2);
            }

        }else{
            loginUser.setUsername(user.getuName());
            loginUser.setPassword(user.getuPassword());
            loginUser.setRole(1);
            loginUser.setEmail(user.getuEmail());
        }
        return loginUser;
    }
}
